package com.neborosoft.FlyingDog.android.gui.adapters.listview;

import android.content.Context;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;
import com.neborosoft.FlyingDog.R;
import com.neborosoft.FlyingDog.logic.database.content.identified.ArtCollection;
import com.neborosoft.FlyingDog.logic.database.content.identified.ArtSize;
import com.neborosoft.android.gui.adapters.listview.ViewArrayAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 01.12.12
 * Time: 17:51
 * To change this template use File | Settings | File Templates.
 */
public class ArtCollectionListAdapter<Element extends ArtCollection>
        extends ViewArrayAdapter<Element,ArtCollectionViewHolder>
{
    private static final ArtSize ART_SIZE = ArtSize.MEDIUM;
    private static final ImageLoader IMAGE_LOADER = ImageLoader.getInstance();

    private int artWidth;
    private int artHeight;

    public ArtCollectionListAdapter(Context context, AbsListView parent) {
        super(context, parent);
    }

    @Override
    protected int getRootLayoutId() {
        return R.layout.art_collection_layout;
    }

    @Override
    protected int getNullLayoutId() {
        return R.layout.loading_cell_layout;
    }

    @Override
    protected void reuseView(Element element,
                                    ArtCollectionViewHolder viewHolder,
                                    int position)
    {
        String name = element.getName();
        viewHolder.name.setText(name);

        String url = element.getArtUrl(ART_SIZE);
        IMAGE_LOADER.displayImage(url,viewHolder.art);
    }

    @Override
    protected void onFirstViewMeasured(ArtCollectionViewHolder viewHolder) {
        artHeight = viewHolder.art.getHeight();
        artWidth = viewHolder.art.getWidth();
    }

    @Override
    protected ArtCollectionViewHolder createViewHolder(View view) {
        ArtCollectionViewHolder viewHolder = new ArtCollectionViewHolder();
        viewHolder.name = (TextView)view.findViewById(R.id.artCollectionName);
        viewHolder.art = (ImageView)view.findViewById(R.id.artCollectionArt);

        return viewHolder;
    }
}
